package com.lej.theme.dialog

import android.content.Context
import android.text.method.LinkMovementMethod
import android.widget.Button
import androidx.annotation.DrawableRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.lej.theme.R
import com.lej.theme.databinding.ThemeDialogConfirm2Binding
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.qmuiteam.qmui.kotlin.onClick

class ImgAlertPopDialog(
    context: Context, var title: CharSequence?, var content: CharSequence?,
    var confirmText: CharSequence = "确认",
    var cancelText: CharSequence = "再等等",
    val hideCancel: Boolean = false,
    var showClose: Boolean = false,//显示关闭按钮
    var tipsLogo: Int = 0,//弹窗logo
    val ratio: Float = 1f,//两个按钮的比例
    val cancel: () -> Unit = {},
    val onConfirm: () -> Unit = {}
) : CenterPopupView(context) {
    override fun getImplLayoutId() = R.layout.theme_dialog_confirm2
    override fun initPopupContent() {
        super.initPopupContent()
        val binding =
            ThemeDialogConfirm2Binding.bind(centerPopupContainer.findViewById(R.id.cl_dialog))
        binding.tvTitle.text = title ?: ""
        binding.tvTitle.isGone = title.isNullOrEmpty()
        binding.ivCloseRight.isVisible = showClose
        if (showClose) {
            binding.ivCloseRight.onClick {
                cancel()
                dismiss()
            }
        }
        if (tipsLogo != 0) {
            binding.ivIcon.isGone = false
            binding.ivIcon.setImageResource(tipsLogo)
        }
        if (!content.isNullOrEmpty()) {
            binding.tvSubTitle.text = content
            binding.tvSubTitle.movementMethod = LinkMovementMethod.getInstance()
            binding.tvSubTitle.isGone = false
        } else {
            binding.tvSubTitle.isGone = true
        }
        val btnPositive: Button
        val btnNegative: Button
        if (positiveIndex == 1) {
            btnPositive = binding.btnPositive
            btnNegative = binding.btnNegative
        } else {
            btnPositive = binding.btnNegative
            btnNegative = binding.btnPositive
        }
        btnPositive.text = confirmText
        btnNegative.text = cancelText

        if (ratio != 1f && !hideCancel) {
            val layout = btnNegative.layoutParams as ConstraintLayout.LayoutParams
            layout.horizontalWeight = ratio

        }
        btnNegative.isGone = hideCancel
        btnNegative.onClick {
            cancel()
            dismiss()
        }
        btnPositive.onClick {
            onConfirm()
            dismiss()
        }
        binding.ivCloseRight.onClick {
            dismiss()
        }
    }

    ///积极的按钮 0,1
    var positiveIndex = 1

    companion object {
        fun show(
            context: Context,
            title: CharSequence? = "",
            content: CharSequence? = "",
            confirmText: CharSequence = "确认",
            cancelText: CharSequence = "再等等",
            showClose: Boolean = false,//显示关闭按钮
            @DrawableRes tipsLogo: Int = 0,//弹窗logo
            hasClose: Boolean = true,//是否可以点击空白关闭弹窗
            hideCancel: Boolean = false,
            ratio: Float = 1f,//两个按钮的比例
            cancel: () -> Unit = {},
            onConfirm: () -> Unit = {}
        ): BasePopupView {
            return XPopup.Builder(context)
                .dismissOnTouchOutside(hasClose)
                .dismissOnBackPressed(hasClose)
                .asCustom(
                    ImgAlertPopDialog(
                        context,
                        title = title,
                        content = content,
                        confirmText = confirmText,
                        cancelText = cancelText,
                        hideCancel = hideCancel,
                        showClose = showClose,
                        tipsLogo = tipsLogo,
                        cancel = cancel,
                        ratio = ratio,
                        onConfirm = onConfirm
                    )
                )
                .show()
        }
    }
}